package com.cloudy.core.thrift.client;

import com.cloudy.core.thrift.zookeeper.client.IAddressProvider;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.TServiceClientFactory;

/**
 * Created by vipshop on 16/11/18.
 */
public class ThriftClientPoolableObjectFactory extends BasePoolableObjectFactory<TServiceClient> {

    private final IAddressProvider addressProvider;
    private final TServiceClientFactory<TServiceClient> clientFactory;

    ThriftClientPoolableObjectFactory(IAddressProvider addressProvider,
                                      TServiceClientFactory<TServiceClient> clientFactory) throws Exception {
        this.addressProvider = addressProvider;
        this.clientFactory = clientFactory;
    }

    @Override
    public TServiceClient makeObject() throws Exception {
        ThriftClientBootstrap thriftClientBootstrap =  new ThriftClientBootstrap(addressProvider,clientFactory);
        return thriftClientBootstrap.create();
    }
}
